//
//  TransliterationInputController.m
//  VaramozhiEditor
//
//  Created by Jeesmon Jacob on 3/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "TransliterationInputController.h"

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "mal_api.h"
#include "txt2html.h"
#import "Toast+UIView.h"

#import <Social/Social.h>

#import "QuartzCore/QuartzCore.h"
#import "WebViewController.h"


@interface TransliterationInputController ()

@end

@implementation TransliterationInputController
@synthesize lblMalayalam;
@synthesize editorView;
@synthesize toolBarActions;

bool emailSupported = NO;
bool smsSupported = NO;
bool fbSupported = NO;
bool twitterSupported = NO;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) findDeviceCapability
{
    Class composeViewControllerClass = (NSClassFromString(@"SLComposeViewController"));
    if (composeViewControllerClass != nil) {
        fbSupported = YES;
        twitterSupported = YES;
    }
    
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        // We must always check whether the current device is configured for sending emails
        if ([mailClass canSendMail])
        {
            emailSupported = YES;
        }
        
    }
    
    Class smsClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if(smsClass != nil)
    {
        if ([smsClass canSendText])
        {
            smsSupported = YES;
        }
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:NULL];
        
    //}
        
    self.lblMalayalam.layer.borderWidth = 2;
    self.lblMalayalam.layer.borderColor = [[UIColor darkGrayColor] CGColor];
    self.lblMalayalam.font = [UIFont fontWithName:@"Rachana" size:self.lblMalayalam.font.pointSize];
    
    self.editorView.layer.borderWidth = 2;
    self.editorView.layer.borderColor = [[UIColor darkGrayColor] CGColor];
    
}
- (void) viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [[self navigationController] setNavigationBarHidden:YES animated:NO];

    [self findDeviceCapability];
    [self toggleItems:NO];
}
- (void) viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    [self.editorView becomeFirstResponder];
}
- (void)viewDidUnload
{
    [self setEditorView:nil];
    [self setLblMalayalam:nil];
    [self setToolBarActions:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        
        return YES;
        
    }else{
        return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
    }
    
}
- (void)keyboardWillShowNotification:(NSNotification *)inNotification
{
	
	
	CGRect keyboardBounds;
    [[inNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
	
    CGFloat keyboardHeight;
    switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            keyboardHeight = keyboardBounds.size.height;
            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            keyboardHeight = keyboardBounds.size.width;
            break;
            
    }
	
    NSTimeInterval animationDuration = [[[inNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:animationDuration];
	[UIView setAnimationBeginsFromCurrentState:YES];
    
	CGSize screenSize = self.view.frame.size;
    CGFloat availHeight = screenSize.height - keyboardHeight - 65;
    CGFloat eachHeight = availHeight/2;
        
    //if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        
        [self.lblMalayalam setFrame:CGRectMake(5, 5, screenSize.width-10, eachHeight)];
        [self.toolBarActions setFrame:CGRectMake(5, eachHeight + 10 , screenSize.width-10, 45)];
        [self.editorView setFrame:CGRectMake(5, eachHeight + 10 + 45 + 5, screenSize.width-10, eachHeight)];
    //}
    
    
    
	[UIView commitAnimations];
	
    
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    long flags = FL_DEFAULT;
    NSMutableString *inputStr = nil;
    
	if (range.length == 0){
        
        
        
        inputStr = [NSMutableString stringWithFormat:@"%@",textView.text];
        [inputStr insertString:text atIndex:range.location];
        
    }else{
        
        
        inputStr = [NSString stringWithFormat:@"%@%@",[textView.text substringToIndex:range.location], [textView.text substringFromIndex:range.location+range.length]];
    }
    
    char *output = mozhi_unicode_parse([inputStr UTF8String], flags);
    NSString *outputStr = [NSString stringWithUTF8String:output];
    
    self.lblMalayalam.text = outputStr;
    
    [self.lblMalayalam scrollRangeToVisible:NSMakeRange([self.lblMalayalam.text length], 0)];
    
    BOOL isEnabled = ([self.lblMalayalam.text length] > 0);
    self.navigationItem.leftBarButtonItem.enabled = isEnabled;
    
	return YES;
}

- (void) clearScreen:(id)sender{    
    self.lblMalayalam.text = @"";
    self.editorView.text = @"";
    self.navigationItem.leftBarButtonItem.enabled = NO;
}

- (void) copyToClipboard:(id)sender{
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    
    pasteboard.string = self.lblMalayalam.text;
    
}
- (void) toggleItems: (bool) toggle
{
    
    NSArray *arrItemsExist = self.toolBarActions.items;
    NSInteger firstObject = 0;
    
    if([arrItemsExist count] > 0){
        UIBarButtonItem *itemME = [arrItemsExist objectAtIndex:0];
        
        firstObject = itemME.tag;
    }
    
    NSLog(@"firstObject = %i", firstObject);
    
    NSMutableArray *arrayItems = [NSMutableArray array];
    
    if((toggle && firstObject == kActionFB) || firstObject == 0 || (!toggle && firstObject == kActionClear)){
        
        
        /**** Clear Button ****/
        
        CustomButton *clearButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionClear Title:@"Clear" Image:[UIImage imageNamed:@"remove.png"]];
        clearButton.btndelegate = self;
        
                
        UIBarButtonItem *itemM1 = [[UIBarButtonItem alloc] initWithCustomView:clearButton];
        itemM1.tag = kActionClear;
        [arrayItems addObject:itemM1];
        
        
        UIBarButtonItem *flex1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [arrayItems addObject:flex1];
        
        
        
         /**** Copy Button ****/
        
             
        CustomButton *copyButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionCopy Title:@"Copy" Image:[UIImage imageNamed:@"copy.png"]];

        copyButton.btndelegate = self;
        UIBarButtonItem *itemM2 = [[UIBarButtonItem alloc] initWithCustomView:copyButton];
        itemM2.tag = kActionCopy;
        [arrayItems addObject:itemM2];
        
        
        UIBarButtonItem *flex2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [arrayItems addObject:flex2];
        
         /**** Mail Button ****/
        
        CustomButton *mailButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionMail Title:@"Mail" Image:[UIImage imageNamed:emailSupported ? @"mail.png" : @"mail_gray.png"]];
        
        mailButton.btndelegate = self;
        UIBarButtonItem *itemM3 = [[UIBarButtonItem alloc] initWithCustomView:mailButton];
        itemM3.tag = kActionMail;
        itemM3.enabled = emailSupported;
        [arrayItems addObject:itemM3];
        
        
        
        UIBarButtonItem *flex3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [arrayItems addObject:flex3];
        
        
        /**** SMS Button ****/
        
        CustomButton *smsButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionSMS Title:@"SMS" Image:[UIImage imageNamed: smsSupported ? @"envelope.png" : @"envelope_gray.png"]];
        smsButton.btndelegate = self;
        UIBarButtonItem *itemM4 = [[UIBarButtonItem alloc] initWithCustomView:smsButton];
        itemM4.tag = kActionSMS;
        [arrayItems addObject:itemM4];
        
        
        UIBarButtonItem *flex4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [arrayItems addObject:flex4];
        
        if(![[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
            
            /**** More Button ****/
            
            CustomButton *moreButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionMore Title:@"More" Image:[UIImage imageNamed:@"more.png"]];
            
            moreButton.btndelegate = self;
            UIBarButtonItem *itemM = [[UIBarButtonItem alloc] initWithCustomView:moreButton];
            itemM.tag = kActionMore;
            [arrayItems addObject:itemM];

        }
        
                
        
        
        
    }
    if((toggle && firstObject == kActionClear) || (!toggle && firstObject == kActionFB) || ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)){
        
        CustomButton *fbButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionFB Title:@"Facebook" Image:[UIImage imageNamed: fbSupported ? @"facebook.png" : @"facebook_gray.png"]];
        
        fbButton.btndelegate = self;
        UIBarButtonItem *itemM = [[UIBarButtonItem alloc] initWithCustomView:fbButton];
        itemM.tag = kActionFB;
        
        [arrayItems addObject:itemM];
        
        UIBarButtonItem *flex1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [arrayItems addObject:flex1];
        
        
        CustomButton *twButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionTwitter Title:@"Twitter" Image:[UIImage imageNamed:twitterSupported ? @"twitter.png" : @"twitter_gray.png"]];
        twButton.btndelegate = self;
        itemM = [[UIBarButtonItem alloc] initWithCustomView:twButton];
        itemM.tag = kActionTwitter;
        [arrayItems addObject:itemM];
        
        UIBarButtonItem *flex2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [arrayItems addObject:flex2];
        
        
        CustomButton *infoButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionAbout Title:@"Info" Image:[UIImage imageNamed:@"info.png"]];
        infoButton.btndelegate = self;        
        UIBarButtonItem *itemM1 = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
        itemM1.tag = kActionAbout;
        [arrayItems addObject:itemM1];
        
        UIBarButtonItem *flex3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [arrayItems addObject:flex3];
        
        
        CustomButton *helpButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionHelp Title:@"Help" Image:[UIImage imageNamed:@"question_mark.png"]];
        helpButton.btndelegate = self;
        
        UIBarButtonItem *itemM2 = [[UIBarButtonItem alloc] initWithCustomView:helpButton];
        itemM2.tag = kActionHelp;
        [arrayItems addObject:itemM2];
        
        if(![[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
        
            
            UIBarButtonItem *flex4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            [arrayItems addObject:flex4];
        
        
            
            UIBarButtonItem *flex5 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            [arrayItems addObject:flex5];
            
            
            CustomButton *moreButton = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, 30, 35) Tag:kActionMore Title:@"More" Image:[UIImage imageNamed:@"more.png"]];
            
            moreButton.btndelegate = self;            
            UIBarButtonItem *itemM = [[UIBarButtonItem alloc] initWithCustomView:moreButton];
            itemM.tag = kActionMore;
            [arrayItems addObject:itemM];

        }
        
                
        
        
    }
    /*for (UIBarButtonItem * item in arrayItems)
    {
        if(item.tag > 0){
            item.style = UIBarButtonItemStylePlain;
            item.style = UIBarButtonItemStyleBordered;
        }
    }*/
    self.toolBarActions.items = arrayItems;
    
    

}

- (void)showAlertMessage:(NSString *) message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}

#pragma mark ButtonClickDelegate

- (void) buttonClicked:(NSInteger)actionTag{
    
    NSLog(@"actionTag = %i", actionTag);
    
    NSString *enteredText = self.lblMalayalam.text;
    
    if(actionTag == kActionClear){
        
        self.lblMalayalam.text = @"";
        self.editorView.text = @"";
        
    }else if(actionTag == kActionCopy){
        
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        
        pasteboard.string = self.lblMalayalam.text;
        
        [self.view makeToast:@"Text copied. Paste anywhere." duration:2.0 position:@"top"];
        
    }else if(actionTag == kActionMail){
        
        if (emailSupported)
        {
            [self displayComposerSheet:enteredText];
        }
        
    }else if(actionTag == kActionFB){
        if(fbSupported) {
            SLComposeViewController *objvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
            
            //setting the text to post
            [objvc setInitialText:enteredText];
            
            
            [self presentViewController:objvc animated:YES
                             completion:nil];
            
            objvc.completionHandler = ^(SLComposeViewControllerResult result)
            {
                 if (result == SLComposeViewControllerResultDone) {
                     [self.view makeToast:@"Text posted to Facebook" duration:2.0 position:@"top"];
                 }
                /*
                 else if (result == SLComposeViewControllerResultCancelled)
                 {
                 UIAlertView *objresponseAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Action cancelled" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                 [objresponseAlert show];
                 }
                 */
                [self.editorView becomeFirstResponder];
            };
        }
        else {
            [self showAlertMessage:@"iOS6+ feature"];
        }

        
    }else if(actionTag == kActionTwitter){
        if(twitterSupported) {
            SLComposeViewController *objvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
            
            //setting the text to post
            [objvc setInitialText:enteredText];
            
            
            [self presentViewController:objvc animated:YES
                             completion:nil];
            
            objvc.completionHandler = ^(SLComposeViewControllerResult result)
            {
                 if (result == SLComposeViewControllerResultDone) {
                     [self.view makeToast:@"Text posted to Twitter" duration:2.0 position:@"top"];
                 }
                /*
                 else if (result == SLComposeViewControllerResultCancelled)
                 {
                 UIAlertView *objresponseAlert = [[UIAlertView alloc]initWithTitle:nil message:@"Action cancelled" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                 [objresponseAlert show];
                 }
                 */
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self dismissViewControllerAnimated:NO completion:^{
                        NSLog(@"Tweet Sheet has been dismissed.");
                        [self.editorView becomeFirstResponder];
                    }];
                });
                
                //[self.editorView becomeFirstResponder];+20130909
            };
        }
        else {
            [self showAlertMessage:@"iOS6+ feature"];
        }

        
    }else if(actionTag == kActionSMS){
        if (smsSupported)
        {
            [self presentMessageComposeViewController:enteredText];
        }
        else {
            [self showAlertMessage:@"Device does not support SMS"];
        }
    }else if(actionTag == kActionHelp){
        
        WebViewController *webViewCtrlr = [[WebViewController alloc] init];
        webViewCtrlr.requestURL = [[NSBundle mainBundle] pathForResource:@"lipi" ofType:@"png"];
        [self.navigationController pushViewController:webViewCtrlr animated:YES];
        
    }else if(actionTag == kActionAbout){
        
        WebViewController *webViewCtrlr = [[WebViewController alloc] init];
        webViewCtrlr.requestURL = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"html"];
        [self.navigationController pushViewController:webViewCtrlr animated:YES];
    }else if(actionTag == kActionMore){
        
        [self toggleItems:YES];
    }

}
/*
- (void)  performAction:(id)sender{
    
        
    NSMutableArray *arrayItems = [NSMutableArray arrayWithCapacity:5];
    
    
    //if([UIDeviceHardware isOS6Device]){
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
        [arrayItems addObject:NSLocalizedString(@"post.twitter", @"")];
        
    }
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]){
        
        [arrayItems addObject:NSLocalizedString(@"post.facebook", @"")];
    }

    
    if([self.lblMalayalam.text length] > 0){
        
        
        [arrayItems addObject:NSLocalizedString(@"post.email", @"")];
        
    }
    [arrayItems addObject:NSLocalizedString(@"help", @"")];
    [arrayItems addObject:NSLocalizedString(@"about", @"")];
    
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];
    actionSheet.tag = 1;
    
    for(NSString *item in arrayItems){
        
        [actionSheet addButtonWithTitle:item];
    }
    
    [actionSheet addButtonWithTitle:NSLocalizedString(@"Cancel", @"")];
    [actionSheet setCancelButtonIndex:[arrayItems count]];
    actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
    [actionSheet showInView:self.navigationController.view]; 
    
}

#pragma mark UIActionSheetDelegate methods

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
	
	
	
	if(buttonIndex != [actionSheet cancelButtonIndex]){
        
        NSString *enteredText = self.lblMalayalam.text;
        NSString *btnTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
        
        
        if([btnTitle isEqualToString:NSLocalizedString(@"post.email", @"")]){
            
            //Email
            
            Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
            if (mailClass != nil)
            {
                // We must always check whether the current device is configured for sending emails
                if ([mailClass canSendMail])
                {
                    [self displayComposerSheet:enteredText];
                }
                
            }
            
        }
        else if([btnTitle isEqualToString:NSLocalizedString(@"help", @"")]){
            
            WebViewController *webViewCtrlr = [[WebViewController alloc] init];
            webViewCtrlr.requestURL = [[NSBundle mainBundle] pathForResource:@"lipi" ofType:@"png"];
            [self.navigationController pushViewController:webViewCtrlr animated:YES];
        }
        else if([btnTitle isEqualToString:NSLocalizedString(@"about", @"")]){
            
            WebViewController *webViewCtrlr = [[WebViewController alloc] init];
            webViewCtrlr.requestURL = [[NSBundle mainBundle] pathForResource:@"info" ofType:@"html"];
            [self.navigationController pushViewController:webViewCtrlr animated:YES];
        }
        else if([btnTitle isEqualToString:NSLocalizedString(@"post.twitter", @"")]){
            
            SLComposeViewController *objvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
            
            //setting the text to post
            [objvc setInitialText:enteredText];
            
            
            //[objvc addImage:];
            
            
            //[objvc addURL:[NSURL URLWithString:@""]];
            
            [self presentViewController:objvc animated:YES
                             completion:nil];
            
            objvc.completionHandler = ^(SLComposeViewControllerResult result)
            {
                if (result == SLComposeViewControllerResultDone) {
                    UIAlertView *objresponseAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"application.name", @"") message:@"Your data is posted on Twitter" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [objresponseAlert show];
                }
                else if (result == SLComposeViewControllerResultCancelled)
                {
                    UIAlertView *objresponseAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"application.name", @"") message:@"Twitter Data posting cancelled" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [objresponseAlert show];
                }
            };
        }
        else if([btnTitle isEqualToString:NSLocalizedString(@"post.facebook", @"")]){
            
            SLComposeViewController *objvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
            
            //setting the text to post
            [objvc setInitialText:enteredText];
            
            
            //[objvc addImage:];
            
            
            //[objvc addURL:[NSURL URLWithString:@""]];
            
            [self presentViewController:objvc animated:YES
                             completion:nil];
            
            objvc.completionHandler = ^(SLComposeViewControllerResult result)
            {
                if (result == SLComposeViewControllerResultDone) {
                    UIAlertView *objresponseAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"application.name", @"") message:@"Your data is posted on Facebook wall" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [objresponseAlert show];
                }
                else if (result == SLComposeViewControllerResultCancelled)
                {
                    UIAlertView *objresponseAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"application.name", @"") message:@"Facebook Data posting cancelled" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [objresponseAlert show];
                }
            };
            
        }
    }
    
    
}
 */
#pragma mark private methods
-(void)displayComposerSheet:(NSString *)text{
	
    
    
    
    NSMutableString *emailBody = [[NSMutableString alloc] init ];
    
    
    [emailBody appendFormat:@"%@",text];
    
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
	
	[picker setSubject:@"Shared from Varamozhi"];
	
	[picker setMessageBody:emailBody isHTML:NO];
    
    [[picker navigationBar] setTintColor:[UIColor colorWithHue:.32 saturation:.39 brightness:.70 alpha:0.4]];
	
	[self.navigationController presentModalViewController:picker animated:YES];
	
	
}

#pragma mark  MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
    
    switch (result)
	{
		case MFMailComposeResultCancelled:
			//(@"Result: canceled");
			break;
		case MFMailComposeResultSaved:
			//(@"Result: saved");
            
			break;
		case MFMailComposeResultSent:
			//(@"Result: sent");
            
			break;
		case MFMailComposeResultFailed:
			//(@"Result: failed");
			break;
		default:
			//(@"Result: not sent");
			break;
	}
    [[self navigationController] dismissModalViewControllerAnimated:YES];
    
}

-(void) presentMessageComposeViewController:(NSString *)text{
    id smsViewController = [[NSClassFromString(@"MFMessageComposeViewController") alloc] init];
    [smsViewController setTitle:NSLocalizedString(@"Shared from Varamozhi",nil)];
    [smsViewController setBody:NSLocalizedString(text,nil)];
    [smsViewController setMessageComposeDelegate: (id)self];
    [[smsViewController navigationBar] setTintColor:[UIColor colorWithHue:.32 saturation:.39 brightness:.70 alpha:0.4]];
    [self presentModalViewController:smsViewController animated:YES];
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    [self dismissModalViewControllerAnimated:NO];
    [self.editorView becomeFirstResponder];
}

@end


@implementation UIImage (INResizeImageAllocator)
+ (UIImage*)imageWithImage:(UIImage*)image
			  scaledToSize:(CGSize)newSize;
{
	UIGraphicsBeginImageContext( newSize );
	[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
	UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	
	return newImage;
    /*
     CGSize size = CGSizeMake(image.size.width * scaleBy, image.size.height * scaleBy);
     
     UIGraphicsBeginImageContext(size);
     CGContextRef context = UIGraphicsGetCurrentContext();
     CGAffineTransform transform = CGAffineTransformIdentity;
     
     transform = CGAffineTransformScale(transform, scaleBy, scaleBy);
     CGContextConcatCTM(context, transform);
     
     // Draw the image into the transformed context and return the image
     [image drawAtPoint:CGPointMake(0.0f, 0.0f)];
     UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     
     return newimg;
     */
}
- (UIImage*)scaleImageToSize:(CGSize)newSize
{
	return [UIImage imageWithImage:self scaledToSize:newSize];
}

@end
